﻿using System;
using System.Collections.Generic;

namespace HamcoWcs.Models.DeviceMsgs.SrmMsgs
{
    /// <summary>
    /// 堆垛机Teach数据消息 [WCS&lt;-&gt;PLC]<br/>
    /// </summary>
    /// <remarks>
    /// Request:<br/>
    /// 0:Wcs已读<br/>
    /// 1:查询<br/>
    /// 2:Srm返回查询数据<br/>
    /// 3:Srm查询失败<br/>
    /// </remarks>
    public class SrmTeachMsg : MsgBase
    {
        public ushort Row { get; set; }
        public ushort Bay { get; set; }
        public ushort Lev { get; set; }
        public ushort Deep { get; set; }
        public ushort Station { get; set; }
        public float WalkPosition { get; set; }
        public float LiftHighPosition { get; set; }
        public float LiftLowPosition { get; set; }
        public float ForkPosition { get; set; }
        public ushort Request { get; set; }

        public SrmTeachMsg(bool isReserve = true) : base(isReserve)
        {
        }

        public SrmTeachMsg(byte[] msg, bool isReserve = true) : base(isReserve)
        {
            Decode(msg);
        }




        public void Decode(byte[] msg)
        {
            Row = BitConverter.ToUInt16(GetBytes(msg, 0, 2));
            Bay = BitConverter.ToUInt16(GetBytes(msg, 2, 2));
            Lev = BitConverter.ToUInt16(GetBytes(msg, 4, 2));
            Deep = BitConverter.ToUInt16(GetBytes(msg, 6, 2));
            Deep = BitConverter.ToUInt16(GetBytes(msg, 8, 2));
            WalkPosition = BitConverter.ToSingle(GetBytes(msg, 10, 4));
            LiftHighPosition = BitConverter.ToSingle(GetBytes(msg, 14, 4));
            LiftLowPosition = BitConverter.ToSingle(GetBytes(msg, 18, 4));
            ForkPosition = BitConverter.ToSingle(GetBytes(msg, 22, 4));
            Request = BitConverter.ToUInt16(GetBytes(msg, 26, 2));
        }

        public byte[] Encode()
        {
            var bytes = new List<byte>();
            bytes.AddRange(Reserve(BitConverter.GetBytes(Row)));
            bytes.AddRange(Reserve(BitConverter.GetBytes(Bay)));
            bytes.AddRange(Reserve(BitConverter.GetBytes(Lev)));
            bytes.AddRange(Reserve(BitConverter.GetBytes(Deep)));
            bytes.AddRange(Reserve(BitConverter.GetBytes(Station)));
            bytes.AddRange(Reserve(BitConverter.GetBytes(WalkPosition)));
            bytes.AddRange(Reserve(BitConverter.GetBytes(LiftHighPosition)));
            bytes.AddRange(Reserve(BitConverter.GetBytes(LiftLowPosition)));
            bytes.AddRange(Reserve(BitConverter.GetBytes(ForkPosition)));

            bytes.AddRange(BitConverter.GetBytes(Reserve(Request)));
            return bytes.ToArray();
        }
    }
}
